package com.lunevich.layoutanimation;

import static com.nineoldandroids.view.ViewPropertyAnimator.animate;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class AndroidLayoutAnimationActivity extends Activity implements OnClickListener {

	private final int MOVE_DURATIOH = 2000;
	private final int SCALE_DURATIOH = 500;
	// private final int REMOVE_DURATIOH = SCALE_DURATIOH;
	private final int BUTTONS_LIMIT = 4;
	private int btnCount = 0;

	private Button btnAdd;
	private Button fadeOut;
	private Button btnUp1;
	private Button btnUp2;

	private List<Button> buttons = new ArrayList<Button>();

	private LinearLayout bottomLayout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		btnAdd = (Button) findViewById(R.id.btnAdd);
		fadeOut = (Button) findViewById(R.id.fadeOut);
		btnUp1 = (Button) findViewById(R.id.btnUp1);
		btnUp2 = (Button) findViewById(R.id.btnUp2);

		btnAdd.setOnClickListener(this);
		fadeOut.setOnClickListener(this);
		btnUp1.setOnClickListener(this);

		bottomLayout = (LinearLayout) findViewById(R.id.bottomLayout);

		buttons.add(fadeOut);
		buttons.add(btnUp1);
		buttons.add(btnUp2);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.fadeOut:
			animate(fadeOut).alpha(0);
			for (int i = 0; i < buttons.size() - 1; i++) {
				int moveToY;
				if (i > 0) {
					moveToY = -buttons.get(i).getTop() + i * buttons.get(i - 1).getTop();
				} else {
					moveToY = -buttons.get(i).getTop();
				}
				animate(buttons.get(i + 1)).setDuration(MOVE_DURATIOH).translationY(moveToY);
			}
			break;

		case R.id.btnAdd:
			if (bottomLayout.getChildCount() < BUTTONS_LIMIT) {
				final CustomButton btn = new CustomButton(this);
				btn.setText("Button " + btnCount);
				btn.setId(btnCount);
				btn.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						moveBottomButton(v, btn.getId());
					}
				});
				bottomLayout.addView(btn);
				btnCount++;
			} else {
				Toast.makeText(AndroidLayoutAnimationActivity.this, "Button limit", Toast.LENGTH_LONG).show();
			}
			break;
		case R.id.btnUp1:
			Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();
			break;
		}
	}

	private void moveBottomButton(View v, int btnId) {
		int btnCount = bottomLayout.getChildCount() - 1;

		final CustomButton curButton = (CustomButton) findViewById(btnId);

		animate(curButton).setDuration(SCALE_DURATIOH).scaleX(0);

		if (btnId != btnCount) {
			for (int i = btnId; i < btnCount; i++) {
				Button temp = (Button) findViewById(i);
				Button prev = (Button) findViewById(i - 1);
				Button next = (Button) findViewById(i + 1);
				int moveToY = 0;
				if (i > 0 && bottomLayout.getChildAt(0).getId() != i) {
					moveToY = -temp.getLeft() + prev.getLeft();
				} else {
					moveToY = -next.getLeft();
				}
				animate(next).setDuration(MOVE_DURATIOH).translationX(moveToY);
			}
		}
		
		// bottomLayout.removeView(curButton);
	}
}